package com.zhilianbao.leyaogo.view.me;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.bql.pickphotos.widget.HorizontalListView;


/**
 * Created by liu on 2017/1/5.
 */

public class MyOrderHorizontalListView extends HorizontalListView {

    private Context mContext;

    public MyOrderHorizontalListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }


    @Override
    protected boolean dispatchHoverEvent(MotionEvent event) {
        return true;
    }

    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        //动态设置listView的宽度
//        int totalWidth = 0;
//        for (int i = 0; i < getAdapter().getCount(); i++) {
//            View listItem = getAdapter().getView(i, null, this);
//            listItem.measure(0, 0);
//            totalWidth += listItem.getMeasuredWidth();
//        }
//
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) ev.getX();
                downY = (int) ev.getY();
                break;
//            case MotionEvent.ACTION_MOVE:
//                float moveX = ev.getX();
//                float moveY = ev.getY();
//                float disY = moveY - interceptY;
//                float disX = moveX - interceptX;
//                if (interceptX > totalWidth) {
//                    getParent().getParent().requestDisallowInterceptTouchEvent(false);
//                }else {
//                    if (Math.abs(disX) < Math.abs(disY)) {//纵向滑动
//                       getParent().getParent().requestDisallowInterceptTouchEvent(false);
//                    } else {
//                        getParent().getParent().requestDisallowInterceptTouchEvent(true);
//                    }
//                }
//                break;
//            case MotionEvent.ACTION_UP:
//                getParent().getParent().requestDisallowInterceptTouchEvent(false);
//                break;
        }
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.onInterceptTouchEvent(ev);
    }


    int downX;
    int downY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //动态设置listView的宽度
        int totalWidth = 0;
        boolean canScroll = false;
        for (int i = 0; i < getAdapter().getCount(); i++) {
            View listItem = getAdapter().getView(i, null, this);
            listItem.measure(0, 0);
            totalWidth += listItem.getMeasuredWidth();
        }
//        downX = (int) event.getX();
//        downY = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                canScroll = false;
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getX();
                float moveY = event.getY();
                float disY = moveY - downY;
                float disX = moveX - downX;
                if (downX > totalWidth) {
                    getParent().requestDisallowInterceptTouchEvent(false);
                    canScroll = false;
                } else if (Math.abs(disX) < Math.abs(disY)) {//纵向滑动
                    getParent().requestDisallowInterceptTouchEvent(false);
                    canScroll = false;
                } else {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    canScroll = true;
                }
                break;
            case MotionEvent.ACTION_UP:
                getParent().requestDisallowInterceptTouchEvent(false);
                canScroll = false;
                break;
        }
        return canScroll;
    }

 /*   // 分别记录上次滑动的坐标
    private int mLastX = 0;
    private int mLastY = 0;
    // 分别记录上次滑动的坐标(onInterceptTouchEvent)
    private int mLastXIntercept = 0;
    private int mLastYIntercept = 0;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean intercepted = false;
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                intercepted = false;
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                int deltaX = x - mLastXIntercept;
                int deltaY = y - mLastYIntercept;
                if (Math.abs(deltaX) > Math.abs(deltaY)) {
                    intercepted = true;
                } else {
                    intercepted = false;
                }
                break;
            }
            case MotionEvent.ACTION_UP: {
                intercepted = false;
                break;
            }
            default:
                break;
        }

        mLastX = x;
        mLastY = y;
        mLastXIntercept = x;
        mLastYIntercept = y;

        return intercepted;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                int deltaX = x - mLastX;
                int deltaY = y - mLastY;
                if (Math.abs(deltaX) > Math.abs(deltaY)) {
                    return true;
                } else {
                   return false;
                }
            }
            case MotionEvent.ACTION_UP: {
                return false;

            }
        }

        mLastX = x;
        mLastY = y;
        return super.onTouchEvent(event);
    }
*/
}
